Track override chain assemblies in symbol-based mode#143
Merged
Conversation
In the symbol-based analysis path, member access via an `override` only credited the override's containing assembly. The C# compiler validates the entire override chain at compile time, so any assembly that declares an overridden base member must remain a reference -- removing it produces CS0012 errors. Symbol-based RT, on `IPropertyReferenceOperation` / `IInvocationOperation` / `IEventReferenceOperation`, walked only `Member.ContainingAssembly` (= override's assembly) and never visited `Member.OverriddenProperty?.ContainingAssembly` (= base's assembly), so the consumer's reference to the base assembly was wrongly flagged RT0002 removable. Fix: add a `TrackOverriddenChain` helper that walks `OverriddenMethod` / `OverriddenProperty` / `OverriddenEvent` chains and tracks every containing assembly. Call it from the `IInvocationOperation` and `IMemberReferenceOperation` branches. Fields don't have override chains. Adds five regression tests that all fail without this change: - `UsedViaOverridePropertyAccess`: the canonical scenario - `UsedViaOverrideMethodCall`: method variant - `UsedViaOverrideEventAccess`: event variant - `UsedViaOverrideMultiLevelChain`: three-level A -> B -> C chain - `UnrelatedReferenceNotMarkedByOverride`: negative test ensuring the fix doesn't over-credit unrelated assemblies Different code path from #141 (qualifier tracking on inherited static member access) and #142 (delegate parameter / return types). Surfaced by a QuickBuild trial of the symbol-based mode. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
dfederm
added a commit
that referenced
this pull request
Apr 30, 2026
…145) In the symbol-based analysis path, only the immediate base type's containing assembly was credited via `TrackType(namedType.BaseType)`. The C# compiler validates the entire base-type chain plus implemented interfaces at type-check time -- CS0012 fires when *any* link's defining assembly is missing -- so any assembly along the chain must remain a reference. With `<DisableTransitiveProjectReferences>true</...>`, the chain doesn't flow transitively, so the consumer must explicitly reference grandparent assemblies. Symbol-based RT, when handling `INamedTypeSymbol`, walked only the immediate `BaseType` and direct `Interfaces`, never recursing further up. So for `Consumer : Provider` where `Provider : ProviderDependency` in another assembly, the consumer's reference to `ProviderDependency` was wrongly flagged RT0002 removable. Fix: extend `TrackType`'s `INamedTypeSymbol` branch to walk the full `BaseType` chain and `AllInterfaces` collection. `AllInterfaces` is broader than `Interfaces` (includes transitively-implemented interfaces) and mirrors what the compiler validates. A `ConcurrentDictionary<ISymbol, byte>` keyed via `SymbolEqualityComparer.Default` gates the chain walk to break self-referential cycles such as `int -> AllInterfaces[IComparable<int>] -> typeArg int -> ...` and to avoid redundant work. Adds seven regression tests that all fail without this change: - `UsedViaInheritedBaseType`: the canonical issue #144 scenario - `UsedViaImplementedInterface`: interface in the chain - `UsedViaMultiLevelInheritanceChain`: three-level A <- B <- C - `UsedViaInheritanceChainOnVariableType`: chain via parameter type - `UsedViaMixedBaseAndInterfaceChain`: base + interface, four asms - `UsedViaGenericConstraintBaseChain`: `where T : Provider` constraint - `UnrelatedReferenceNotMarkedByInheritance`: negative test ensuring the fix doesn't over-credit unrelated assemblies Verified against upstream repro https://github.com/olstakh/RT_Gap_Inheritance: `Consumer` builds with 0 RT warnings (was emitting RT0002 with v3.5.2). Different code path from #141 (qualifier tracking on inherited static member access), #142 (delegate parameter / return types), and #143 (override chains). Same family of "symbol traversal misses a chain" gaps; same reporter (olstakh). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
In the symbol-based analysis path (
ReferenceTrimmerUseSymbolAnalysis=true), member access via anoverrideonly credited the override's containing assembly. The C# compiler validates the entire override chain at compile time, so any assembly that declares an overridden base member must remain a reference -- removing it producesCS0012errors.The bug
csharp // Assembly A public abstract class Base { public abstract string SomeProperty { get; } } // Assembly B (refs A) public class Derived : Base { public override string SomeProperty => ""value""; } // Assembly C (refs B and A) var s = new Derived().SomeProperty; // C# compiler validates the full override chainSymbol-based RT, on
IPropertyReferenceOperation/IInvocationOperation/IEventReferenceOperation, walked onlyMember.ContainingAssembly(= override's assembly = B) and never visitedMember.OverriddenProperty?.ContainingAssembly(= A). So C's reference to A was wrongly flaggedRT0002removable.Surfaced by a QuickBuild trial:
QAnalysisLibTestsaccessesGuardianArtifactPublisher.AdoContainerName, which overrides an abstract member declared onAdoArtifactUtilsinVstsTaskApiClient. The default mode (GetUsedAssemblyReferences) keeps the reference; symbol-based flagged it removable; removing it produced 39CS0012errors.Fix
Add a
TrackOverriddenChainhelper that walksOverriddenMethod/OverriddenProperty/OverriddenEventchains and tracks every containing assembly. Call it from theIInvocationOperation(TargetMethod) andIMemberReferenceOperation(Member) branches insideRegisterOperationAction. Fields don't have override chains, so they're skipped via the type switch.Distinct from #141 and #142
Foo.StaticMethod()whereStaticMethodis inherited - trackFoo's assembly). Override chains are the symmetric direction: the override is the ""near"" symbol, the base is the ""far"" assembly that needs tracking.INamedTypeSymbolbranch onRegisterSymbolAction. Override-chain gap fires on instance member access inRegisterOperationAction, not on type declarations.Tests
Five new regression tests in
AnalyzerTests, all of which fail without this change:UsedViaOverridePropertyAccess-- canonical scenario from the QB trialUsedViaOverrideMethodCall-- method variantUsedViaOverrideEventAccess-- event variantUsedViaOverrideMultiLevelChain-- three-level A -> B -> C chain ensures the loop walks past the immediate overrideUnrelatedReferenceNotMarkedByOverride-- negative test ensuring the fix doesn't over-credit unrelated assembliesVerified by stashing the analyzer change and rerunning: every new test fails without the fix. Full suite: 147 / 150 pass; the 3 remaining failures are pre-existing C++ MSVC E2E tests unrelated to this change.
Out of scope
Implicit and explicit interface implementations have similar shape (compiler validates the implementation relationship) but use
ISymbol.ExplicitInterfaceImplementations/FindImplementationForInterfaceMemberrather thanOverriddenX. The QB trial did not produce a clear repro for that case, so it's deliberately not addressed here. If a real-world repro surfaces it should be a separate fix.